<?php
namespace src;
use classlib\input;
use src\logic\Cadmin;
use classlib\core as C;
use classlib\core\controller;
use src\logic\lshare;
class Cplatformstats extends controller {
	public function main() {
		$view   = C\view::inst('extjs');
		$userid = Cadmin::chkaccess($view);
		$view->assign_by_ref('title', Cadmin::$name);
		$view->display('live/stat/platformstats.ext.js');
	}

	public function dolist() {
		$view   = C\view::inst('json');
		Cadmin::chkaccess($view);
		list($start, $limit, $sort) = Cadmin::offset();
		$record = $this->record($start, $limit, $sort);
		$view->assign_by_ref('total', $record['total']);
		$view->assign_by_ref('dataset', $record['list']);
		$view->display();
	}

	private function record($start, $limit, $sort = '') {
		$stype  = (int)input::get_post('stype');
		$where  = array('statistic'=>$stype);
		$sdate  = input::post('sdate');
		if ($sdate) {
			$where[] = array('stime', strtotime($sdate), orm::OP_GT);
		}
		$edate  = input::post('edate');
		if ($edate) {
			$where[] = array('stime', strtotime($edate), orm::OP_LT);
		}
		$orm    = C\model::load('pro.sti.countdata');
		$total  = $orm->get_total($where);
		$record = array('total'=>0, 'list'=>array());
		if ($total) {
			$record['list'] = $orm->get_list($start, $limit, $where, '*', $sort);
			foreach ($record['list'] as &$item) {
				$item['charge_count'] = '$'.$item['charge_count'] / lshare::CANRATIO;
				$item['charge_total_count']   = '$'.$item['charge_total_count'] / lshare::CANRATIO;
				$item['charge_total_web']     = '$'.$item['charge_total_web'] / lshare::CANRATIO;
				$item['charge_total_android'] = '$'.$item['charge_total_android'] / lshare::CANRATIO;
				$item['charge_total_ios']     = '$'.$item['charge_total_ios'] / lshare::CANRATIO;
				$item['stime'] = date('Y-m-d H:i:s', $item['stime']);
			}
			$record['total'] = $total;
		}
		return $record;
	}

	public function doexport() {
		$_POST['sort'] = 'id';
		$_POST['dir']  = 'DESC';
		$record = $this->record(0, -1);
		ob_end_clean();
		if (empty($record)) {
			exit;
		}
		$data = array();
		foreach ($record['list'] as $item) {
			$data[] = array(
					$item['day'],
					$item['register'],
					$item['login'],
					$item['live_num'],
					$item['live_time'],
					$item['cost_count'],
					$item['cost_num'],
					$item['charge_count'],
					$item['charge_num'],
					$item['charge_total_count'],
					$item['charge_total_web'],
					$item['charge_total_android'],
					$item['charge_total_ios'],
					$item['charge_total_num'],
					$item['remain_coin'],
					$item['remain_earn']
			);
		}
		$header = array('Day/Week/Month','Register(注册数)', 'Login(登录数)', 'LiveUser(直播人数)', 'LiveTime(直播总时长)', 'CostCoin(消费新币)', 'CostUser(消费人数)',
				'PayMoney(充值金额)', 'ChargeUser(充值人数)', 'PayTotalMoney(总充值金额)', 'PayTotalWeb(web总充值金额)', 'PayTotalAndroid(安卓总充值金额)',
				'PayTotalIos(苹果总充值金额)', 'PayTotalUser(总充值人数)', 'RemainCoin(全部用户剩余新币)', 'RemainEarn(全部主播剩余收益没领取)'
		);
		$view = C\view::inst('execl');
		$view->assign('header', $header);
		$view->assign('data', $data);
		$view->display();
	}
}